Add support for unescaping html entities to the javascript agent

Rachel Brindle 10 年之前
父节点
当前提交
20a2e7486a
共有 2 个文件被更改,包括 19 次插入0 次删除
  1. 5 0
      app/models/agents/java_script_agent.rb
  2. 14 0
      spec/models/agents/java_script_agent_spec.rb

+ 5 - 0
app/models/agents/java_script_agent.rb

@@ -102,6 +102,7 @@ module Agents
102 102
           memory.to_json
103 103
         end
104 104
       end
105
+      context["unescapeHTML"] = lambda { |a, x| CGI.unescapeHTML(x) }
105 106
 
106 107
       context.eval(code)
107 108
       context.eval("Agent.#{js_function}();")
@@ -158,6 +159,10 @@ module Agents
158 159
           doError(message);
159 160
         }
160 161
 
162
+        Agent.unescapeHTML = function(html) {
163
+          return unescapeHTML(html);
164
+        }
165
+
161 166
         Agent.check = function(){};
162 167
         Agent.receive = function(){};
163 168
       JS

+ 14 - 0
spec/models/agents/java_script_agent_spec.rb

@@ -162,6 +162,20 @@ describe Agents::JavaScriptAgent do
162 162
       end
163 163
     end
164 164
 
165
+    describe "unescaping HTML" do
166
+      it "can unescape html with this.unescapeHTML in the javascript environment" do
167
+        @agent.options['code'] = 'Agent.check = function() { this.createEvent({ message: this.unescapeHTML(\'test "escaping" <characters>\'), stuff: { foo: 5 } }); };'
168
+        @agent.save!
169
+        expect {
170
+          expect {
171
+            @agent.check
172
+          }.not_to change { AgentLog.count }
173
+        }.to change { Event.count}.by(1)
174
+        created_event = @agent.events.last
175
+        expect(created_event.payload).to eq({ 'message' => 'test "escaping" <characters>', 'stuff' => { 'foo' => 5 }})
176
+      end
177
+    end
178
+
165 179
     describe "getting incoming events" do
166 180
       it "can access incoming events in the JavaScript enviroment via this.incomingEvents" do
167 181
         event = Event.new